local WHITE = "|cffffffff"
local SILVER = "|cffc0c0c0"
local GREEN = "|cff00ff00"
local LTBLUE = "|cffa0a0ff"
local g_lastaction = ""
CE.Boss_Only = false
	
function DIYCE_DebugSkills(skillList)
    DEFAULT_CHAT_FRAME:AddMessage(GREEN.."Skill List:")
    
    for i,v in ipairs(skillList) do
        DEFAULT_CHAT_FRAME:AddMessage(SILVER.."  ["..WHITE..i..SILVER.."]: "..LTBLUE.."\" "..WHITE..v.name..LTBLUE.."\"  use = "..WHITE..(v.use and "true" or "false"))
    end

    DEFAULT_CHAT_FRAME:AddMessage(GREEN.."----------")
end

function DIYCE_DebugBuffList(buffList)
    DEFAULT_CHAT_FRAME:AddMessage(GREEN.."Buff List:")
    
    for k,v in pairs(buffList) do
        -- We ignore numbered entries because both the ID and name 
        -- are stored in the list. This avoids doubling the output.
        if type(k) ~= "number" then
            DEFAULT_CHAT_FRAME:AddMessage(SILVER.."  ["..WHITE..k..SILVER.."]:  "..LTBLUE.."id: "..WHITE..v.id..LTBLUE.."  stack: "..WHITE..v.stack..LTBLUE.."  time: "..WHITE..(v.time or "none"))
        end
    end
    
    DEFAULT_CHAT_FRAME:AddMessage(GREEN.."----------")    
end

-- List of Mobs you dont wana target
local IgnorableMobs = {
        ["Hand of Truth Researcher"] = true, -- DoD Suicide mobs
        ["Hand of Truth Apothecary"] = true, -- DoD Suicide mobs
		["Spirit of the Oak"] = true, -- warden pet
		["Oak Walker"] = true, -- warden pet
		["Nature Crystal"] = true, -- warden pet
        -- Add more names to this list in same fashion
						}
-- List of Boss skill you wana use Physic protect food
CE.protectList = {
                    }
-- List off Boss skill you wana silence					
CE.silenceList = {
        [TEXT("Sys490244_name")]	= true,	-- Thunderstorm
		[TEXT("Sys493364_name")]	= true, -- Storm Erosion > Evelin goblin mine's
		[TEXT("Sys493665_name")]	= true, -- King Bug Shock > Life Leacher in Origin
        [TEXT("Sys490635_name")]	= true,	-- Mana Rift
		[TEXT("Sys491088_name")]	= true,	-- Dream of Gold
		[TEXT("Sys490629_name")]	= true,	-- Flame
		[TEXT("Sys492803_name")]	= true,	-- Flame Spell
        [TEXT("Sys493109_name")]	= true,	-- Wave Bomb
		[TEXT("Sys490077_name")]	= true,	-- Silence
        [TEXT("Sys493528_name")]	= true,	-- Recover
        [TEXT("Sys493526_name")]	= true,	-- Restore Life
        [TEXT("Sys490261_name")]	= true,	-- Heal
		[TEXT("Sys494323_name")]	= true,	-- Healing Ripple
		[TEXT("Sys492636_name")]	= true,	-- Curing Shot
		[TEXT("Sys495611_name")]	= true,	-- Leaves of Fire
		[TEXT("Sys491147_name")]	= true,	-- Urgent Heal
		[TEXT("Sys497021_name")]	= true,	-- Heavy Shelling > Juggler Apprentice in Grafu
		[TEXT("Sys497654_name")]	= true,	-- Dark Healing > Mini-boss in Sardo
		[TEXT("Sys495372_name")]	= true,	-- Iron Wing > Furious Wodjin
		[TEXT("Sys850828_name")]	= true, -- Flame Jet > Spider Mandara
		[TEXT("Sys624300_name")]	= true,	-- Explosive Burn > Spider Mandara
		[TEXT("Sys850825_name")]	= true,	-- Burning Fireball > Spider Mandara
		[TEXT("Sys498606_name")]	= true,	-- Crackling Flash > AC chest mobs
		[TEXT("Sys498375_name")]	= true,	-- Ancestral Fury > Tosh 3th boss
		[TEXT("Sys498108_name")]	= true, -- Targonharl's Fury > Worldboss Targonharl
        [TEXT("Sys850171_name")]	= true,	-- Storm's Pulse > Kulech 3th boss
		[TEXT("Sys498455_name")]	= true,	-- Myrmex Survival > Kulech 5th boss
		[TEXT("Sys498409_name")]	= true,	-- Amplified Punishment > Kulech 6th boss
		[TEXT("Sys498312_name")]	= true,	-- Myrmex Chain > Kulech mob's
		[TEXT("Sys850509_name")]	= true,	-- Gravity: Life Leech > Bethonia 4 boss 
		[TEXT("Sys850547_name")]	= true,	-- Light Lock > Bethonia mob's
		[TEXT("Sys850973_name")]	= true,	-- Yolita's kiss > 2th boss Belathis
		[TEXT("Sys850854_name")]	= true,	-- Outward Whirlwind
		[TEXT("Sys493659_name")]	= true,	-- Starving Blade
		[TEXT("Sys499356_name")]	= true,	-- Summon Underworld Shadow > 7'th boss belathis
		[TEXT("Sys499357_name")]	= true,	-- Deathly Attack > 7'th boss belathis
			}
-- Rogue Substitue this list                    
CE.subList = {
		["Sharp Slash"] = true, -- 1st Boss DOD
		["Conjure Energy"] = true, -- 4th boss GC HM AOE
		["Cat Claw Whirlwind"] = true,  --1st boss RT AOE
		["Void Fire"] = true, --2nd boss RT AOE
			}

function KillSequence(arg1, goat2)
--arg1 = "v1" or "v2" for debugging
--mode = used for various purposes, such as setting custom sections for specific situation. (IE: Seige War/PvP)
--healthpot = # of actionbar slot for health potions
--manapot = # of actionbar slot for mana potions
--foodslot = # of actionbar slot for food (add more args for more foodslots if needed)

    local Skill = {}
    local Skill2 = {}
    local i = 0
    
    -- Player and target status.
    local enemy = UnitCanAttack("player","target")
	local playerHP = UnitHealth("player")
    local tbuffs = BuffList("target")
    local pbuffs = BuffList("player")
    local tDead = UnitIsDeadOrGhost("target")
    local LockedOn = UnitExists("target")
    local party = GetNumPartyMembers() >= 2
	local MobName = UnitName("target")
	
	-- Combat engin config locals
	local EqSwapChek = CE_Settings[CE.playerName]["Tab2"]["Eq_Protect_Check"]
	local PetProtectChek = CE_Settings[CE.playerName]["Tab2"]["Pet_Protect_Check"]
	local HpAlarmChek = CE_Settings[CE.playerName]["Tab2"]["HP_Alarm_Check"]
	local EqSwap = tonumber(CE_Settings[CE.playerName]["Tab2"]["Eq_Protect"])
	local PetProtect = tonumber(CE_Settings[CE.playerName]["Tab2"]["Pet_Protect"])
	local HpAlarm = tonumber(CE_Settings[CE.playerName]["Tab2"]["HP_Alarm"])
	local SwapTo = tonumber(CE_Settings[CE.playerName]["Tab2"]["Swap_to_Slot"])
	
    --Record skills
    local tSpell,tTime,tElapsed = UnitCastingTime("target")
	
	if tSpell then
		local tRounded = round(tTime)
		CE.EnemySkillMessage("["..tSpell.."] "..tRounded.."s", 1, 1, 1)
		if CE.record and (UnitSex("target") >= 2) then
			if tTime > 0 then
				local AddSkill
				if EnemySkills[MobName] == nil then
					EnemySkills[MobName] = {}
					EnemySkills[MobName][k] = tSpell
					DEFAULT_CHAT_FRAME:AddMessage(MobName.." table added")
				end
				for k,v in pairs(EnemySkills[MobName]) do
					if tSpell == v then
						AddSkill = false
						break
					else
						AddSkill = true
					end
				end
				if AddSkill then
					table.insert(EnemySkills[MobName], tSpell)
					DEFAULT_CHAT_FRAME:AddMessage("Added "..MobName.." and skill ["..tSpell.."]")
				end
			end
		end
	end
	
	--Potion Checks
    healthpot = healthpot or 0
    manapot = manapot or 0
    
	--Skip named mobs
	if MobName and IgnorableMobs[MobName] then
		TargetNearestEnemy()
		return
	end
	
    --Equipment Protection
	if (EqSwapChek == true) then
		if playerHP <= EqSwap then
			if SwapTo == 1 then
				SwapEquipmentItem(0)
			elseif SwapTo == 2 then
				SwapEquipmentItem(1)
			elseif SwapTo == 3 then
				SwapEquipmentItem(2)
			end
			SendSystemMsg("|H|h|cffFF0000"..CE_Locales.Diyce.SaveEq.."|h")
		end		
	end
	
	--Pet Protection
	if (PetProtectChek == true) then
		if playerHP <= PetProtect then
			for i=1,6 do
				if (IsPetSummoned(i) == true) then
					ReturnPet(i);
					SendSystemMsg("|H|h|cffFF0000"..CE_Locales.Diyce.SavePet.."|h")
				end
			end 
		end
	end
	
	-- Health warnning
	if (HpAlarmChek == true) then
		if playerHP <= HpAlarm then
			SendSystemMsg("|H|h|cffFF0000"..CE_Locales.Diyce.LowHp.."|h")
			PlaySoundByPath("interface/Addons/CombatEngin/Sounds/Alarm.mp3")
		end
	end
	
    --Check for level 1 mobs, if it is, drop target and acquire a new one.
	if (LockedOn and (UnitLevel("target") < 2)) then
        TargetUnit()
        return
    end
	
	-- Check if target is dead
    if (tDead) then
        TargetUnit("")
		g_lastaction = ""
        return
    end
	
	--Determine Class-Combo
    mainClass, subClass = UnitClassToken( "player" )
	
	if mainClass == "THIEF" then
		CE.Rogue(arg1, goat2)
	elseif mainClass == "RANGER" then
		CE.Scout(arg1, goat2)
	elseif mainClass == "MAGE" then
		CE.Mage(arg1, goat2)
	elseif mainClass == "KNIGHT" then
		CE.Knight(arg1, goat2)
	elseif mainClass == "DRUID" then
		CE.Druid(arg1, goat2)
	elseif mainClass == "AUGUR" then
		CE.Priest(arg1, goat2)
	elseif mainClass == "WARDEN" then
		CE.Warden(arg1, goat2)
	elseif mainClass == "WARRIOR" then
		CE.Warrior(arg1, goat2)
	elseif mainClass == "PSYRON" then
		CE.Champion(arg1, goat2)
	elseif mainClass == "HARPSYN" then
		CE.Warlock(arg1, goat2)
	end
    --End Player Skill Sequences
    
    if (arg1=="debugskills") then        --Used for printing the skill table, and true/false usability
        DIYCE_DebugSkills(Skill)
        DIYCE_DebugSkills(Skill2)
    elseif (arg1=="debugpbuffs") then    --Used for printing your buff names, and buffID
        DIYCE_DebugBuffList(pbuffs)
    elseif (arg1=="debugtbuffs") then    --Used for printing target buff names, and buffID
        DIYCE_DebugBuffList(tbuffs)
    elseif (arg1=="debugall") then        --Used for printing all of the above at the same time
        DIYCE_DebugSkills(Skill)
        DIYCE_DebugSkills(Skill2)
        DIYCE_DebugBuffList(pbuffs)
        DIYCE_DebugBuffList(tbuffs)
    end
    
    -- Target options
	if CE.Boss_Only == true then
		for i=1,10 do
			if UnitSex("target") >= 3 then
				break
			end
			TargetNearestEnemy()
		end
		if 3 > UnitSex("target") then
			TargetUnit("")
			return
		end
	elseif goat2 == "pvp" then
		for i=1,10 do
			if UnitIsPlayer("target") then
				break
			end
			TargetNearestEnemy()
		end
		if UnitCanAttack("player","target") then
			if UnitIsPlayer("target") ~= true then
				TargetUnit("")
				return
			end
		else
			TargetUnit("")
			return
		end
	elseif mainClass == "RANGER" and (not party) then        --To keep scouts from pulling mobs without meaning to.
		if (not LockedOn) or (not enemy) then
			TargetNearestEnemy()
			return
		end
	elseif mainClass ~= "RANGER" then                    --Let all other classes auto target.
		if (not LockedOn) or (not enemy) then
			TargetNearestEnemy()
			return
		end
	end
end

function TargetThis(_arg)
	for i=1,10 do
		if UnitName("target") == _arg then
			break
		end
		TargetNearestEnemy()
	end
end

function TargetThis1()
	local name=UnitName("target")
	for i=1,20 do
		if name == "Holy Shield of Trail" then
			break
		else
			TargetNearestEnemy()
		end
	end
	if name ~= "Holy Shield of Trail" then 
		TargetUnit("")
	end
end
